---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[
    基于起点，和终点，做出一条线，并沿这条线 创建 激光实体

    以0.5间距创建。最后不足0.5的时候，以末尾来创建
]]--
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
---
    local DELTA_LENGTH = 0.5
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


local function fn()
    local inst = CreateEntity()
    inst.entity:AddTransform()
    inst.entity:AddAnimState()
    inst.entity:AddSoundEmitter()
    inst.entity:AddNetwork()
    inst:AddTag("FX")
    inst:AddTag("NOCLICK")
    inst.entity:SetPristine()
    if not TheWorld.ismastersim then
        return inst
    end
    inst:ListenForEvent("Set",function(inst,_table)
        -----------------------------------------------------------------------------------------------
        ---
            -- _table = _table or {
            --     start_pt =  Vector3(0,0,0),
            --     end_pt = Vector3(0,0,0),
            --     colour = {255,255,255},
            --     sound = "",
            --     delay_frame = 0,                --- 下一个创建的激光，需要等待的帧数
            --     onhit_delay_frame = 0,        --- 激光击中目标后，需要等待的帧数
            --     onhitfn = function(x,y,z) end,    --- 激光击中目标后，调用的函数
            --     delta_length = 0.5,
            -- }
        -----------------------------------------------------------------------------------------------
        ---
            local start_pt = _table.start_pt
            local end_pt = _table.end_pt
            local delay_frame = _table.delay_frame or 0
            local colour = _table.colour or {0,0,255}
            local onhit_delay_frame = _table.onhit_delay_frame or 0
            local onhitfn = _table.onhitfn or function() end
            local delta_length = _table.delta_length or DELTA_LENGTH
        -----------------------------------------------------------------------------------------------
        --- 归一化向量,并计算需要的坐标组。以DELTA_LENGTH间距创建。最后不足DELTA_LENGTH的时候，以末尾来创建
            local points = {}
            local dir = (end_pt - start_pt):Normalize()
            local len = (end_pt - start_pt):Length()
            local i = 0
            while i < len do
                local pt = start_pt + dir * i
                table.insert(points,pt)
                i = i + DELTA_LENGTH
            end
            table.insert(points,end_pt)
        -----------------------------------------------------------------------------------------------
        --- 创建实体逻辑
            local function CreateSingleFx(inst,pt)
                local fx = SpawnPrefab("loramia_sfx_single_laser_beam")
                fx:PushEvent("Set",{
                    pt = pt,
                    colour = colour,
                    sound = _table.sound,
                })
                fx:DoTaskInTime(FRAMES*onhit_delay_frame,function(fx)
                    onhitfn(pt.x,0,pt.z)
                end)
            end
            if delay_frame == 0 then
                ---- 无延迟一次性创建
                for _,pt in ipairs(points) do
                    CreateSingleFx(inst,pt)
                end
            else
                ---- 延迟创建
                for i,pt in ipairs(points) do
                    inst:DoTaskInTime(FRAMES*(delay_frame*(i-1)),CreateSingleFx,pt)
                end
            end
        -----------------------------------------------------------------------------------------------
        ---
            local max_time = math.max(0,FRAMES*(delay_frame*(i-1)))
            inst:DoTaskInTime(max_time+10,inst.Remove)
        -----------------------------------------------------------------------------------------------
        inst.Ready = true
    end)
    inst:DoTaskInTime(0,function(inst)
        if not inst.Ready then
            inst:Remove()
        end
    end)
    return inst
end
return Prefab("loramia_spell_line_laser_beam",fn)


----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
--[[
        SpawnPrefab("loramia_spell_line_laser_beam"):PushEvent("Set",{
            start_pt = Vector3(x,y,z),
            end_pt = Vector3(x+10,y,z+15),
            colour = {255,255,0},
            delta_length = 1,
            delay_frame = 0.4,
            -- onhit_delay_frame = 10,
            -- onhitfn = function(x,y,z)

            -- end,
        })
]]--
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------